package com.sparrow.rpc.future;

import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

/**
 * 异步设置结果的future
 * @param <T>
 */
public class SettableRpcFuture<T> extends RpcFuture<T> {

    private CompletableFuture<T> future = new CompletableFuture<>();

    public void addListener(BiConsumer<T, Throwable> action) {
        future.whenComplete(action);
    }

    /**
     * 主动设置结果
     */
    public boolean setSuccess(T result) {
        return future.complete(result);
    }

    public boolean setFailure(Throwable ex) {
        return future.completeExceptionally(ex);
    }

    public static <T> SettableRpcFuture<T> of() {
        return new SettableRpcFuture();
    }

    public boolean isDone() {
        return future.isDone();
    }


}
